package com.jy.app.lib.view.uilistview;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.jy.app.lib.R;


public class UILoadingLayout extends FrameLayout {
	/**
	 * 加载完成后，显示数据
	 */
	public static final int S_ShowData = -2;
	/**
	 * 提示“加载中...”
	 */
	public static final int S_Net_Loading = -1;
	/**
	 * 未连接
	 */
	public static final int S_Net_UnConnect = 0;
	/**
	 * 网络连接失败
	 */
	public static final int S_Net_Fail = 1;
	/**
	 *服务器超时
	 */
	public static final int S_Net_TimeOut = 2;
	/**
	 * 提示"网络好像有点不给力"
	 */
	public static final int S_Data_Fail = 3;
	/**
	 * 提示“抱歉！加载失败，请重试”
	 */
	public static final int S_Normal_Fail = 4;
	/**
	 *提示“没有找到符合条件的结果”
	 */
	public static final int S_No_Data_Fail = 5;
	/**
	 *提示“没有找到符合条件的结果”
	 */
	public static final int S_Filter_No_Data_Fail = 6;
	/**
	 *提示“没有找到符合条件的结果”
	 */
	public static final int S_Other = 7;

	private View loading_LayoutInflater;
	private TextView loadingtxt;
	protected View error_layout;
	protected TextView load_layout_refreash_btn;
	protected TextView load_layout_error_text;
	private boolean show_error_layout = true;
	private int loading_layout;
	private int error_layout_net_unconnect; // f
	private int error_layout_net_fail; // g
	private int error_layout_net_timeout; // h
	private int error_layout_data_fail; // i
	private int error_layout_other; // j
	private int error_layout_normal_fail; // k
	private int error_layout_no_data_fail; // l
	private int error_layout_filter_no_data_fail; // m
	private String LAYOUT_INFLATER = Context.LAYOUT_INFLATER_SERVICE;
	private Context context;
	private OnClickListener refreashListener;
	private OnClickListener Listener;

	public UILoadingLayout(Context paramContext) {
		super(paramContext);
		// initView();
	}

	public UILoadingLayout(Context paramContext, AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);
		context = paramContext;
		if (paramAttributeSet != null) {
			int[] CtripLoadingLayout = { R.attr.loading_layout, // 0
					R.attr.show_error_layout, // 1
					R.attr.error_layout_net_unconnect, // 2
					R.attr.error_layout_net_fail, // 3
					R.attr.error_layout_net_timeout, // 4
					R.attr.error_layout_data_fail, // 5
					R.attr.error_layout_normal_fail, // 6
					R.attr.error_layout_other, // 7
					R.attr.error_layout_no_data_fail, // 8
					R.attr.error_layout_filter_no_data_fail // 9
			};
			TypedArray localTypedArray = paramContext.obtainStyledAttributes(
					paramAttributeSet, CtripLoadingLayout);
			if (localTypedArray != null) {
				show_error_layout = localTypedArray.getBoolean(1, false);
				loading_layout = localTypedArray.getResourceId(0,
						R.layout.w_loading_layout);
				error_layout_net_unconnect = localTypedArray.getResourceId(2,
						R.layout.w_load_error_layout_net_unconnect_default);
				error_layout_net_fail = localTypedArray.getResourceId(3,
						R.layout.w_load_error_layout_net_fail_default);
				error_layout_net_timeout = localTypedArray.getResourceId(4,
						R.layout.w_load_error_layout_net_timeout_default);
				error_layout_data_fail = localTypedArray.getResourceId(5,
						R.layout.w_load_error_layout_net_data_fail_default);
				error_layout_other = localTypedArray.getResourceId(7,
						R.layout.w_load_error_layout_nodata_default);
				error_layout_normal_fail = localTypedArray.getResourceId(6,
						R.layout.w_load_error_layout_normal_default);
				error_layout_no_data_fail = localTypedArray.getResourceId(8,
						R.layout.w_load_error_layout_nodata_default);
				error_layout_filter_no_data_fail = localTypedArray
						.getResourceId(9,
								R.layout.w_load_error_layout_nodata_default);
				localTypedArray.recycle();
			}
		}
		// initView();
	}

	public void showStatus(int statusCode) {
		if ((error_layout != null))
			removeView(error_layout);
		switch (statusCode) {
		case S_ShowData:
			if (loading_LayoutInflater != null) {
				loading_LayoutInflater.setVisibility(View.GONE);
			}
			return;
		case S_Net_Loading:
			if (loading_LayoutInflater == null) {
				loading_LayoutInflater = ((LayoutInflater) getContext()
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
						loading_layout, null);
				addView(loading_LayoutInflater, new LayoutParams(
						LayoutParams.MATCH_PARENT,
						LayoutParams.MATCH_PARENT, Gravity.CENTER));
			}
			loading_LayoutInflater.setVisibility(View.VISIBLE);
			loading_LayoutInflater.bringToFront();
			return;
		case S_Net_UnConnect:
			error_layout = ((LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE)).inflate(error_layout_net_unconnect,
					null);
			break;
		case S_Net_Fail:
			error_layout = ((LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE)).inflate(error_layout_net_fail, null);
			break;
		case S_Net_TimeOut:
			error_layout = ((LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE)).inflate(error_layout_net_timeout, null);
			break;
		case S_Data_Fail:
			error_layout = ((LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE)).inflate(error_layout_data_fail, null);
			break;
		case S_Normal_Fail:
			error_layout = ((LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE)).inflate(error_layout_normal_fail, null);
			break;
		case S_No_Data_Fail:
			error_layout = ((LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE))
					.inflate(error_layout_no_data_fail, null);
			error_layout.setOnClickListener(Listener);
			break;
		case S_Filter_No_Data_Fail:
			error_layout = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
					error_layout_filter_no_data_fail, null);
			error_layout.setOnClickListener(Listener);
			break;
		default:
			error_layout = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(error_layout_other, null);
			break;
		}

		if ((show_error_layout) && (error_layout != null)) {
			if (loading_LayoutInflater != null) {
				loading_LayoutInflater.setVisibility(View.GONE);
			}
			// set listener to refreash
			load_layout_refreash_btn = (TextView) error_layout
					.findViewById(R.id.load_layout_refreash_btn);
			if (load_layout_refreash_btn != null) {
				load_layout_refreash_btn.setOnClickListener(refreashListener);
			}
			addView(error_layout, new LayoutParams(
					LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT, Gravity.CENTER));
			error_layout.setVisibility(View.VISIBLE);
			error_layout.bringToFront();
		}
		try {
			load_layout_error_text = (TextView) findViewById(R.id.load_layout_error_text);
		}catch (Exception e){

		}
	}

	public void setErrorText(String error){
		if (!TextUtils.isEmpty(error)&&load_layout_error_text!=null){
			load_layout_error_text.setText(error);
		}
	}

	public void setRefreashListener(OnClickListener onClickListener) {
		this.refreashListener = onClickListener;
	}
	public void setListener(OnClickListener onClickListener) {
		this.Listener = onClickListener;
	}

}
